import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(
          appBar: AppBar(
            title: Text('开关、复选框'),
          ),
          body: const MyHomePage()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // 开关
  bool _on = true;
  String _text = "开";

  onSwitchChanged(value) {
    setState(() {
      _text = value ? "开" : "关";
      _on = value;
    });
  }

  // 复选框
  bool _isAgree = false;
  String _textAgree = "不同意";

  onCheckboxChanged(value) {
    setState(() {
      _isAgree = value;
      _textAgree = value ? "同意" : "不同意";
    });
  }

  // 单选框
  int _selectedValue = 0;
  String _textRadio1 = "是";
  String _textRadio2 = "否";
  onRadioChanged(value) {
    setState(() {
      _selectedValue = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    // return Center(
    //   child: Container(
    //     child: Text('Hello World'),
    //   ),
    // );
    return Scaffold(
      appBar: AppBar(
        title: const Text('开关、复选框_inner'),
      ),
      body: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Switch(value: _on, onChanged: onSwitchChanged),
              Text("${_text}")
            ],
          ),
          Row(
            children:  <Widget>[
              Checkbox(value: _isAgree, onChanged: onCheckboxChanged),
              Text("${_textAgree}")
            ],
          ),
          Row(
            children: <Widget>[
              Radio(
                value: 1,
                groupValue: _selectedValue,
                onChanged: onRadioChanged,
              ),
              Text("${_textRadio1}"),
              Radio(
                value: 2,
                groupValue: _selectedValue,
                onChanged: onRadioChanged,
              ),
              Text("${_textRadio2}"),
            ],
          )
        ],
      ),
    );
  }
}
